//<SCRIPT>
function Files(params){
	this.__fileID = "lblFile";
	this.__pageID = "divPage";
	this.__pagesID = "divPages";
	this.__fileContID = "contFile";
	this.__container = "container";

	this.overFile = overFile;
	this.preSelect = preSelect;
	this.mouseDown = mouseDownFile;
	this._selectFile = _selectFile;
	this._select = _select;
	this._isBrother = _isBrother;
	this._isFile = _isFile;
	this._isPage = _isPage;
	this._isSelected = _isSelected;
	this._oldFile = _oldFile;
	this._selectFile = _selectFile;
	this._selectPage = _selectPage;
	this._fileCont = _fileCont;
	this._fileLabel = _fileLabel;
	this._fileName = _fileName;
	this._pagesCont = _pagesCont;
	this._pageCount = _pageCount;
	this._pageNum = _pageNum;
	this._container = _container;
	this._contID = _contID;
	this.getNode = getNode;
	this.getFileNode = getFileNode;
	this.getPageNode = getPageNode;
	this.initPageTag = initPageTag;
	this.appendPage = appendPage;
	this.showPages = showPages;
	this.deletePage = deletePage;

	var p;
	for( p in params )
		this[p] = params[p];
	///
	//******* Event handlers *******
	function mouseDownFile(){
		this._select();
	}
	function overFile(out){var e = this.getFileNode();	if( !e )return;	e.style.textDecorationUnderline = !out;}
	function preSelect(e){
		e = e || event.srcElement;
		if( event.button == 2 )return;
		if( !this._isPage(e) && !this._isFile(e) )return;
		var r = document.body.createTextRange(); r.moveToElementText(e); r.select();
	}
	//******* Methods *******
	function showPages(e,show){
		e = e || event.srcElement;
		if( !defined(show) )return this._pagesCont(e).style.display==""?true:false;
		this._pagesCont(e).style.display = !show?"none":"";
	}
	function initPageTag(e){e.id = this.__pageID;	return e;}
	function getFileNode(){return this.getNode(this.__fileID);}
	function getPageNode(){return this.getNode(this.__pageID);}
	function getNode(id){
		var e = window.event.srcElement.id == id ? window.event.srcElement : null;
//		if( e ) window.event.cancelBubble = true;
		return e;
	}
	// ***** Select *****
	function _select(e,not){
		if( typeof(e) == "number" ){
			e = selectHist(e);
			if( !e )return;
		}
		e = e || event.srcElement;
		try{var r = document.body.createTextRange(); r.moveToElementText(e); r.findText('~~'); r.select();}catch(e){}
		if( this._isFile(e) )return this._selectFile(e,not);
		if( this._isPage(e) )return this._selectPage(e,not);
	}
	function _selectFile(e,not){
		e = e || this.getFileNode();
		if( !e )return;
		if( e == -1 )e = _selectFile.__selectFile_old1;
		if( !this._isFile(e) )e = this._fileLabel(e);
		e.style.backgroundColor = not ? "" : "navy";
		var old = selectHist();
		if( old && old != e )old.style.backgroundColor = "";
		if( !not )selectHist(e);
	}
	function _selectPage(e,not){
		e = e || this.getPageNode();
		if( !e )return;
		e.style.backgroundColor = not ? "" : "gray";
		var old = selectHist();
		if( old && old != e )old.style.backgroundColor = "";
		if( !not )selectHist(e);
	}
function selectHist(e){
var aa = new Array();
	if( !selectHist.hist )selectHist.hist = new Array();
	if( typeof(e) == "object" ){
		selectHist.hist.unshift(e);
		if(selectHist.hist.length > 10)selectHist.hist.pop();
		e = 0;
	}
	if( e < 0 )selectHist.hist = selectHist.hist.slice(0,-e);
	else selectHist.hist[e||0];
	debugger;
	status = "selectHist.hist.length:"+selectHist.hist.length;
}
	// ***** Page *****
	function appendPage(e,pg){this._pagesCont(e).appendChild(this.initPageTag(pg))}
	function deletePage(e){
		var p = this._pageNum(e,Math.max(this._pageCount(e)-1,0));
		if(p)p.parentElement.removeChild(p);
	}
	//******* Properties *******
	function _isSelected(e){
		e = e || event.srcElement;
		if( this._isFile(e) )return e.style.backgroundColor?true:false;
		if( this._isPage(e) )return e.style.backgroundColor?true:false;
	}
	function _isFile(e){return (e||event.srcElement) && (e||event.srcElement).id == this.__fileID }
	function _isPage(e){return (e||event.srcElement) && (e||event.srcElement).id == this.__pageID }
	function _isBrother(e1,e2){ return this._container(e1) == this._container(e2) }
	function _container(e){return getParentByAttr(e||event.srcElement,this.__container)}
	function _contID(e){return this._container(e).id}
	function _fileCont(e){return getParentByAttr(e||event.srcElement,"id",this.__fileContID)}
	function _pagesCont(e){return this._fileCont(e).all[this.__pagesID]}
	function _fileLabel(e){return this._fileCont(e).all[this.__fileID]}
	function _fileName(e){return this._fileLabel(e).innerText}
	function _pageCount(e){
		var pc = this._pagesCont(e).all[this.__pageID];
		return !pc?0:(pc.length||0);
	}
	function _pageNum(e,num,newNum){
		e = e || event.srcElement;
		if( defined(num) ){
			var pg,pgs = this._pagesCont(e).all[this.__pageID];
			pg = defined(pgs.length)?pgs[num]:pgs;
			if( pg && defined(newNum) )pg.innerText = newNum;
//			if( num >= pgs.length )return alert("There is no page #"+num+" in this file.");
			return pg;
		}
		return isNaN(e.innerText)?alert(e.innerText+" is not a number."):parseInt(e.innerText)-1;
	}
	function _oldFile(e){
		if( defined(e) )_oldFile.__oldFile = e;
		return _oldFile.__oldFile;
	}
}
